package ${buildCodeRequest.codePackageDTO.basePackage}.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.web.base.domain.ResultDO;
import org.web.base.domain.ViewResultHelper;
import org.web.base.domain.helper.ServiceExceptionHelper;
import org.web.base.domain.helper.ValidationHelper;
import org.web.base.domain.view.ViewResultDTO;
<#if buildCodeRequest.jdkVersion != '1.8' && buildCodeRequest.jdkVersion != '8'>
import jakarta.annotation.Resource;
<#else>
import javax.annotation.Resource;
</#if>

import java.util.List;
import java.util.ArrayList;

import ${buildCodeRequest.codePackageDTO.queryDTOPackage}.${buildCodeRequest.codeClassNameDTO.queryDTOClassName};
import ${buildCodeRequest.codePackageDTO.getDtoPackage()}.${buildCodeRequest.codeClassNameDTO.getDtoClassName()};
import ${buildCodeRequest.codePackageDTO.servicePackage}.${buildCodeRequest.codeClassNameDTO.getServiceClassName()};
import ${buildCodeRequest.codePackageDTO.bizPackage}.${buildCodeRequest.codeClassNameDTO.bizClassName};

@Controller("${buildCodeRequest.codeClassNameDTO.initLowerJavaAttributeName}")
@RequestMapping("${buildCodeRequest.codeClassNameDTO.initLowerJavaAttributeName}")
public class ${buildCodeRequest.codeClassNameDTO.controllerClassName} {

    private static final Logger logger = LogManager.getLogger(${buildCodeRequest.codeClassNameDTO.controllerClassName}.class);

    @Resource
    private ${buildCodeRequest.codeClassNameDTO.getServiceClassName()} ${buildCodeRequest.codeClassNameDTO.getServiceObjectName()};

    @Resource
    private ${buildCodeRequest.codeClassNameDTO.getBizClassName()} ${buildCodeRequest.codeClassNameDTO.getBizObjectName()};

    @RequestMapping("${buildCodeRequest.codeMethodDTO.queryListMethod}")
    @ResponseBody
    public ViewResultDTO<List<${buildCodeRequest.codeClassNameDTO.getDtoClassName()}>> ${buildCodeRequest.codeMethodDTO.queryListMethod}(@RequestBody ${buildCodeRequest.codeClassNameDTO.queryDTOClassName} ${buildCodeRequest.codeClassNameDTO.queryDTOObjectName}) {
        ViewResultDTO<List<${buildCodeRequest.codeClassNameDTO.getDtoClassName()}>> viewResultDO = new ViewResultDTO<>(true);
        try {
            // 查询对象校验, 如果调用BIZ层方法，则无需校验，因为BIZ自会进行校验。
            ValidationHelper.validateWithThrow(${buildCodeRequest.codeClassNameDTO.queryDTOClassName}.class, ${buildCodeRequest.codeClassNameDTO.queryDTOObjectName});
            ResultDO<List<${buildCodeRequest.codeClassNameDTO.getDtoClassName()}>> selectResult = ${buildCodeRequest.codeClassNameDTO.getBizObjectName()}.${buildCodeRequest.codeMethodDTO.queryListMethod}(${buildCodeRequest.codeClassNameDTO.queryDTOObjectName});
            viewResultDO.setData(selectResult.getDomain());
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            viewResultDO = ViewResultHelper.buildViewResultByServiceException(e, null);
        }
        return viewResultDO;
    }

    @RequestMapping("save${buildCodeRequest.codeClassNameDTO.getJavaAttributeName()}")
    @ResponseBody
    public ViewResultDTO save${buildCodeRequest.codeClassNameDTO.getJavaAttributeName()}(@RequestBody ${buildCodeRequest.codeClassNameDTO.getDtoClassName()} ${buildCodeRequest.codeClassNameDTO.getDtoObjectName()}) {
        try {
            List<${buildCodeRequest.codeClassNameDTO.getDtoClassName()}> list = new ArrayList<>();
            list.add(${buildCodeRequest.codeClassNameDTO.getDtoObjectName()});
            ResultDO<List<${buildCodeRequest.codeClassNameDTO.getDtoClassName()}>> batchResultDO = ${buildCodeRequest.codeClassNameDTO.getBizObjectName()}.${buildCodeRequest.codeMethodDTO.batchSaveListMethod}(list);
            ResultDO<${buildCodeRequest.codeClassNameDTO.getDtoClassName()}> resultDO = new ResultDO<>();
            if (batchResultDO != null && batchResultDO.isSuccess()) {
                resultDO.setDomain(batchResultDO.getDomain().get(0));
                return ViewResultHelper.convertToViewResultByResultDO(resultDO, ${buildCodeRequest.codeClassNameDTO.getDtoClassName()}.class);
            } else {
                return ViewResultHelper.convertToViewResultByResultDO(batchResultDO, ${buildCodeRequest.codeClassNameDTO.getDtoClassName()}.class);
            }
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            return ViewResultHelper.buildViewResultByServiceException(e, null);
        }
    }


    @RequestMapping("batchDelete${buildCodeRequest.codeClassNameDTO.getJavaAttributeName()}List")
    @ResponseBody
    public ViewResultDTO batchDelete${buildCodeRequest.codeClassNameDTO.getJavaAttributeName()}List(@RequestBody List<Integer> ids) {
        ViewResultDTO viewResultDTO = new ViewResultDTO(false);
        try {
            List<${buildCodeRequest.codeClassNameDTO.getDtoClassName()}> list = new ArrayList<>();
            for (Integer id : ids) {
				${buildCodeRequest.codeClassNameDTO.getDtoClassName()} ${buildCodeRequest.codeClassNameDTO.getDtoObjectName()} = new ${buildCodeRequest.codeClassNameDTO.getDtoClassName()}();
				${buildCodeRequest.codeClassNameDTO.getDtoObjectName()}.set${buildCodeRequest.codeClassNameDTO.getJavaAttributeName()}Id(id);
				${buildCodeRequest.codeClassNameDTO.getDtoObjectName()}.setIsDelete("Y");
				list.add(${buildCodeRequest.codeClassNameDTO.getDtoObjectName()});
            }
			// TDOD , check it, 二选一，逻辑删除还是物理删除。
            //${buildCodeRequest.codeClassNameDTO.getBizObjectName()}.${buildCodeRequest.codeMethodDTO.batchSaveListMethod}(list);
			${buildCodeRequest.codeClassNameDTO.getBizObjectName()}.${buildCodeRequest.codeMethodDTO.batchDeleteMethod}(ids);
            viewResultDTO.setResult(true);
            viewResultDTO.setMsg("批量删除成功");
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            return ViewResultHelper.buildViewResultByServiceException(e, null);
        }
        return viewResultDTO;
    }

}
